/**
 * Copyright [2020] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.callcenter.execute.restful;

import com.hyts.callcenter.base.CallCenterResponse;
import com.hyts.callcenter.engine.delayed.model.ExecuteInvokerEvent;
import com.hyts.callcenter.engine.delayed.redis.DelayedRedissionClientTool;
import com.hyts.callcenter.engine.queue.DefaultExecuteTaskService;
import com.hyts.callcenter.execute.logic.DefaultCallTaskExecute;
import com.hyts.callcenter.extense.event.EventListenerRegistry;
import com.hyts.callcenter.extense.idgen.UUIDGenerator;
import com.hyts.callcenter.model.bo.TaskModel;
import com.hyts.callcenter.model.event.TaskStartEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @project-name:callcenter
 * @package-name:com.hyts.callcenter.engine.web
 * @author:LiBo/Alex
 * @create-date:2021-12-09 12:36 上午
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/engine")
public class EngineController {


    @Autowired
    EventListenerRegistry eventListenerRegistry;


    @Autowired
    UUIDGenerator uuidGenerator;


    @Autowired
    DefaultCallTaskExecute defaultCallTaskExecute;


    @Autowired
    DelayedRedissionClientTool delayedRedissionClientTool;




    @RequestMapping("/addListener")
    public CallCenterResponse addListener(){
        log.info("eventListenerRegistry 发布操作组件:{}",eventListenerRegistry);
        eventListenerRegistry.publish(new TaskStartEvent());
        return CallCenterResponse.success("测试");
    }


    @RequestMapping("/nextId")
    public CallCenterResponse nextId(){
        return CallCenterResponse.success(uuidGenerator.nextId());
    }



    @RequestMapping("/addTaskQueue")
    public CallCenterResponse addTaskQueue(){
        defaultCallTaskExecute.stop(new TaskModel(112L));
        return CallCenterResponse.success();
    }


    @PostMapping("/addDelayedQueue")
    public CallCenterResponse addDelayedQueue(@RequestBody ExecuteInvokerEvent executeInvokerEvent){
        log.info("eventListenerRegistry 发布操作组件:{}",eventListenerRegistry);
        delayedRedissionClientTool.offer(executeInvokerEvent);
        return CallCenterResponse.success(executeInvokerEvent);
    }


    @PostMapping("/addSuperDelayedQueue")
    public CallCenterResponse addSuperDelayedQueue(@RequestBody ExecuteInvokerEvent executeInvokerEvent){
        log.info("eventListenerRegistry 发布操作组件:{}",eventListenerRegistry);
        eventListenerRegistry.publish(new TaskModel(12345678L));
        return CallCenterResponse.success(executeInvokerEvent);
    }


}
